package com.hyy.taobao.controller;

import com.hyy.taobao.api.IUserService;
import com.hyy.taobao.api.entity.User;
import com.hyy.taobao.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/insertUser")
public class InsertUserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        设置客户端发送到服务端 的请求的内容类型
        req.setCharacterEncoding("UTF-8");
//        设置服务端发送到客户端 的响应的内容类型
        resp.setContentType("text/html;utf-8");
        String userName = req.getParameter("userName");
        String userPwd = req.getParameter("userPwd");
        String userQuestion = req.getParameter("userQuestion");
        String userAnswer = req.getParameter("userAnswer");
        User user = new User(userName, userPwd, userQuestion, userAnswer);
        IUserService iUserService = new UserServiceImpl();
        boolean judgement = iUserService.insertUser(user);
        boolean kongJudgement = !"".equals(user.getUserName()) && !"".equals(user.getUserPassword()) && !"".equals(user.getSecurityQuestion()) && !"".equals(user.getSecurityAnswer());
        if (kongJudgement && judgement) {
            req.setAttribute("msg", "注册成功");
            req.getRequestDispatcher("result.jsp").forward(req, resp);
        } else if (!kongJudgement) {
            req.setAttribute("msg", "注册失败,不可以为空");
            req.getRequestDispatcher("result.jsp").forward(req, resp);
        } else if (kongJudgement && !judgement) {
            req.setAttribute("msg", "注册失败，用户名已被占用！");
            req.getRequestDispatcher("result.jsp").forward(req, resp);
        }

    }
}
